<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

class BaseCategoriesBase extends Base {
	var $table = '';
	var $prefix = 'categories_';
	
	//
	var $_table_owner_field = 'owner';
	var $_table_owner_prefix = '';
	var $_table_owner_usable = true;
	var $_table_status_usable = false;
	
	//
	var $format = true;
	var $format_check = false;
	var $format_prefix = 'field_';
	var $item_link = false;
	var $item_link_owner = false;
	var $item_fields = array('id', 'name', 'link');
	var $list_fields = array();
	var $search_fields = array('name');
	var $search_type = 'or';
	var $sort_order = true;
	var $sort_fields = array('sort_order+', 'name+');
	var $filter_fields = array();
	var $filter_map = array();
	var $filter_query = array();
	
	//constructor
	function BaseCategoriesBase($table = '') {
		parent::Base();
		
		if( $table != '' ) {
			$this->table = $table;
		}
	}
	
	//methods
	function getCategory($value, $type = 'id', $check = 0, $exclude = '') {
		$value = func_db_input($value);
		
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str .= " and categories_status = 1";
		}
		if( $exclude != '' ) {
			if( is_array($exclude) ) {
				$query_str .= " and categories_id not in (" . $this->_implode(',', $exclude) . ")";
			} else {
				$query_str .= " and categories_id != '" . (int)$exclude . "'";
			}							  
		}
		if( $type == 'id' || $type == 'categories_id' ) {
			$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and categories_id = '" . (int)$value . "'";
		} elseif( $type == 'link' || $type == 'categories_link' ) {
			$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and categories_link = '" . $value . "'";
		} elseif( $type == 'name' || $type == 'categories_name' ) {
			$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " and categories_name = '" . $value . "'";
		} else {
			return false;
		}
		$query .= $query_str;
		
		return $this->adb->getRow($query);
	}
	
	function getCategoryParent($value, $type = 'id', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			$query_str .= "";
		} else {
			$query_str .= " and ca.categories_status = 1 and cb.categories_status = 1";
		}
		if( $type == 'id' || $type == 'categories_id' ) {
			$query_str .= " and cb.categories_id = '" . (int)$value . "' ";
		} elseif( $type == 'link' || $type == 'categories_link' ) {
			$query_str .= " and cb.categories_link = '" . $value . "' ";
		} elseif( $type == 'name' || $type == 'categories_name' ) {
			$query_str .= " and cb.categories_name = '" . $value . "' ";
		} else {
			return false;
		}
		$query = "select ca.* from " . $this->table . " ca, " . $this->table . " cb where ca.categories_id = cb.categories_parent";
		$query .= $query_str;
		
		return $this->adb->getRow($query);
	}
	
	function getParentCategory($value, $type = 'id', $check = 0) {
		$category = $this->getCategory($value, $type, -1);
		if( is_array($category) && $category['categories_parent'] > 0 ) {
			$category_parent = $this->getCategory($category['categories_parent'], 'id', $check);
			
			return $category_parent;
		}
		
		return false;
	}
	
	function getCategoryAndParent($value, $type = 'id', $check = 0) {
		$category = $this->getCategory($value, $type, $check);
		if( is_array($category) && $category['categories_parent'] > 0 ) {
			$category_parent = $this->getCategory($category['categories_parent'], 'id', $check);
		}
		
		$result = array('category' => $category, 'parent' => $category_parent);
		
		return $result;
	}
	
	function checkCategory($value, $type = 'id', $check = 0) {
		$category = $this->getCategory($value, $type, $check);
		if( $category['categories_id'] > 0 ) {
			return true;
		}
		
		return false;
	}
	
	function checkIsParent($parent, $categories_id) {
		$row = $this->getCategory($categories_id, 'id', -1);
		if( $row['categories_parent'] > 0 ) {
			if( $row['categories_parent'] == $parent ) {
				return true;
			} else {
				return $this->checkIsParent($parent, $row['categories_parent']);
			}
		} else {
			return false;
		}
	}
	
	function checkIsSub($sub_id, $categories_id) {
		return $this->checkIsParent($categories_id, $sub_id);
	}
	
	function checkCategoryParent($categories_id, $parent) {
		if( $parent == 0 ) {
			return true;
		}
		if( $categories_id == $parent ) {
			return false;
		} elseif( $this->checkIsParent($categories_id, $parent) ) {
			return false;
		}
		
		return true;
	}
	
	//
	function getCategoriesA($filter = '', $check = 0, $limit = true) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = $this->_get_query_str($filter, $check);
		$query_order_str = " order by categories_sort_order asc, categories_name asc ";
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. $query_str . $query_order_str . $query_limit_str;
		
		return $this->adb->getRows($query);
	}
	
	function getCategories($filter = '', $check = 0, $limit = true, $include_subs = true, $level = 1) {
		$query_limit_str = "";
		if( $limit == true ) {
			$query_limit_str = $this->getQueryLimitStr();
		}
		
		$query_str = $this->_get_query_str($filter, $check);
		$query_order_str = " order by categories_sort_order asc, categories_name asc ";
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " 
				. $query_str . $query_order_str . $query_limit_str;
		
		$rows = array();
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$row['level'] = $level;
			$rows[] = $row;
			
			if( $include_subs == true ) {
				if( !is_array($filter) ) {
					$filter = array($filter);
				}
				$filter['parent'] = $row['categories_id'];
				$rows_sub = $this->getCategories($filter, $check, $limit, $include_subs, $level + 1);
				if( sizeof($rows_sub) > 0 ) {
					foreach($rows_sub as $key => $row_sub) {
						$rows[] = $row_sub;
					}
				} else {
					//
				}
			}
		}
		
		return $rows;
	}
	
	function getCategoriesTotal($filter = '', $check = 0) {
		$query_str = $this->_get_query_str($filter, $check);
		$query = "select count(*) as count from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str;
		
		return $this->adb->getCount($query);
	}
	
	function getCategoriesList($filter = '', $check = 0, $limit = true, $include_subs = true, $level = 1) {
		$rows = array();
		
		$query_str = $this->_get_query_str($filter, $check);
		$query_order_str = " order by categories_sort_order asc, categories_name asc ";
		$query = "select * from " . $this->table . " where " . $this->_query_owner_rt() . " " . $query_str . $query_order_str;
		
		$result = $this->adb->query($query);
		while($row = $this->adb->fetch_array($result)) {
			$row['level'] = $level;
			$rows[] = $row;
			
			if( $include_subs == true ) {
				if( !is_array($filter) ) {
					$filter = array($filter);
				}
				$filter['parent'] = $row['categories_id'];
				$rows_sub = $this->getCategoriesList($filter, $check, $limit, $include_subs, $level + 1);
				if( sizeof($rows_sub) > 0 ) {
					foreach($rows_sub as $key => $row_sub) {
						$rows[] = $row_sub;
					}
				} else {
					//
				}
			}
		}
		
		return $rows;
	}
	
	function getCategoriesIDsList($filter = '', $check = 0, $limit = true, $include_subs = true, $level = 1) {
		$categories = $this->getCategoriesList($filter, $check, $limit, $include_subs, $level);
		
		if( is_array($categories) ) {
			for($i=0, $n=sizeof($categories); $i<$n; $i++) {
				$categories_ids[] = $categories[$i]['categories_id'];
			}
		}
		
		return $categories_ids;
	}
	
	function getCategoriesTopLevel($filter = '', $check = 0, $limit = true) {
		$categories_top = $this->getCategoriesSubs(0, $filter, $check, $limit);
		
		return $categories_top;
	}
	
	function getCategoriesTopLevelFormat($filter = '', $check = 0, $limit = true) {
		$categories_top = $this->getCategoriesTopLevel($filter, $check, $limit);
		
		$categories_top_format = array();
		if( is_array($categories_top) ) {
			for($i=0, $n=sizeof($categories_top); $i<$n; $i++) {
				$categories_top_format[$categories_top[$i]['categories_id']] = $categories_top[$i]['categories_name'];
			}
		}
		
		return $categories_top_format;
	}
	
	function getCategoriesTwoLevel($parent = 0, $filter = '', $check = 0, $limit = true) {
		$categories_top = $this->getCategoriesTopLevel($filter, $check, $limit);
		for($i=0, $n=sizeof($categories_top); $i<$n; $i++) {
			$categories_top[$i]['subs'] = $this->getCategoriesSubs($categories_top[$i]['categories_id'], $filter, $check, $limit);
		}
		
		return $categories_top;
	}
	
	function getCategoriesThreeLevel($parent = 0, $filter = '', $check = 0, $limit = true) {
		$categories_top = $this->getCategoriesTopLevel($filter, $check, $limit);
		for($i=0, $n=sizeof($categories_top); $i<$n; $i++) {
			$categories_subs = $this->getCategoriesSubs($categories_top[$i]['categories_id'], $filter, $check, $limit);
			for($j=0, $m=sizeof($categories_subs); $j<$m; $j++) {
				$categories_subs[$j]['subs'] = $this->getCategoriesSubs($categories_subs[$j]['categories_id'], $filter, $check, $limit);
			}
			$categories_top[$i]['subs'] = $categories_subs;
		}
		
		return $categories_top;
	}
	
	function getCategoriesSubs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories = $this->getCategories($filter, $check, $limit, true);
		for($i=0, $n=sizeof($categories); $i<$n; $i++) {
			if( $categories[$i]['categories_parent'] == $parent ) {
				$categories_subs[] = $categories[$i];
			}
		}
		
		return $categories_subs;
	}
	
	function getCategoriesSubsFormat($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories = $this->getCategories($filter, $check, $limit, true);
		for($i=0, $n=sizeof($categories); $i<$n; $i++) {
			if( $categories[$i]['categories_parent'] == $parent ) {
				$categories_subs[] = array(
								'id' => $categories[$i]['categories_id'],
								'name' => $categories[$i]['categories_name'],
								'link' => $categories[$i]['categories_link'],
								'parent' => $categories[$i]['categories_parent'],
								'level' => $categories[$i]['level'],
										);
			}
		}
		
		return $categories_subs;
	}
	
	function getCategoriesSubsIDs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories = $this->getCategories($filter, $check, $limit, true);
		for($i=0, $n=sizeof($categories); $i<$n; $i++) {
			if( $categories[$i]['categories_parent'] == $parent ) {
				$categories_subs[] = $categories[$i]['categories_id'];
			}
		}
		
		return $categories_subs;
	}
	
	function getCategoriesAllSubs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories = $this->getCategoriesList($filter, $check, $limit, true);
		$start = false;
		for($i=0, $n=sizeof($categories); $i<$n; $i++) {
			if( $categories[$i]['categories_parent'] == $parent ) {
				$start = true;
				$level = $categories[$i]['level'];
				$categories_subs[] = $categories[$i];
			}
			if( $start == true && $categories[$i]['level'] > $level ) {
				$categories_subs[] = $categories[$i];
			}
		}
		
		return $categories_subs;
	}
	
	function getCategoriesAllSubsIDs($parent = 0, $filter = '', $check = 0, $limit = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories = $this->getCategoriesList($filter, $check, $limit, true);
		$start = false;
		for($i=0, $n=sizeof($categories); $i<$n; $i++) {
			if( $categories[$i]['categories_parent'] == $parent ) {
				$start = true;
				$level = $categories[$i]['level'];
				$categories_subs[] = $categories[$i]['categories_id'];
			}
			if( $start == true && $categories[$i]['level'] > $level ) {
				$categories_subs[] = $categories[$i]['categories_id'];
			}
		}
		
		return $categories_subs;
	}
	
	function getCategoriesTree($filter = '', $parent = 0, $check = 0, $simple = true) {
		if( $filter == '' || !is_array($filter) ) {
			$filter = array();
			$filter['parent'] = $parent;
		} else {
			$filter['parent'] = $parent;
		}
		$categories_tree = $this->getCategoriesList($filter, $check, false, true, '1');
		for($i=0, $n=sizeof($categories_tree); $i<$n; $i++) {
			$prefix = '';
			if( $simple == true ) {
				for($j=1; $j<$categories_tree[$i]['level']; $j++) {
					$prefix .= '&nbsp;&nbsp;&nbsp;';
				}
			} else {
				for($j=1; $j<$categories_tree[$i]['level']; $j++) {
					$prefix .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
				}
				$prefix .= '&nbsp;&nbsp;|&nbsp;--&nbsp;';
			}
			
			$categories_tree[$i]['categories_name_prefix'] = $prefix;
			$categories_tree[$i]['categories_text'] = $prefix . $categories_tree[$i]['categories_name'];
		}
		
		return $categories_tree;
	}
	
	function getCategoriesTreeFormat($filter = '', $parent = 0, $check = 0, $simple = false) {
		$categories_tree = $this->getCategoriesTree($filter, $parent, $check, true);
		
		$categories_tree_format = array();
		if( is_array($categories_tree) ) {
			for($i=0, $n=sizeof($categories_tree); $i<$n; $i++) {
				if( $simple == true ) {
					$categories_tree_format[$categories_tree[$i]['categories_id']] = $categories_tree[$i]['categories_name'];
				} else {
					$categories_tree_format[$categories_tree[$i]['categories_id']] = $categories_tree[$i]['categories_text'];
				}
			}
		}
		
		return $categories_tree_format;
	}
	
	function getCategoriesFormat($filter = '', $check = 0, $limit = true) {
		$categories = $this->getCategories($filter, $check, $limit);
		$categories_format = array();
		if( is_array($categories) ) {
			for($i=0, $n=sizeof($categories); $i<$n; $i++) {
				$categories_format[$categories[$i]['categories_id']] = $categories[$i]['categories_name'];
			}
		}
		
		return $categories_format;
	}
	
	function getFormat($categories, $type = 'tree') {
		$categories_format = array();
		if( is_array($categories) ) {
			for($i=0, $n=sizeof($categories); $i<$n; $i++) {
				if( $type == 'tree' ) {
					if( isset($categories[$i]['categories_text']) ) {
						$categories_format[$categories[$i]['categories_id']] = $categories[$i]['categories_text'];
					} elseif( isset($categories[$i]['categories_name_prefix']) ) {
						$categories_format[$categories[$i]['categories_id']] = $categories[$i]['categories_name_prefix'] . $categories[$i]['categories_name'];
					} else {
						$categories_format[$categories[$i]['categories_id']] = $categories[$i]['categories_name'];
					}
				} else {
					$categories_format[$categories[$i]['categories_id']] = $categories[$i]['categories_name'];
				}
			}
		}
		
		return $categories_format;
	}
	
	function getCategoriesPathA($categories_id, $categories = '', $include_self = true) {
		if( $categories == '' || !is_array($categories) ) {
			$categories = array();
		}
		if( $include_self == true ) {
			$category = $this->getCategory($categories_id, 'id', -1);
			$categories[] = $category;
		}
		if( $categories_id > 0 ) {
			$category_parent = $this->getParentCategory($categories_id, 'id', '-1');
			if( is_array($category_parent) && $category_parent['categories_id'] > 0 ) {
				$categories[] = $category_parent;
				
				$categories = $this->getCategoriesPathA($category_parent['categories_id'], $categories, false);
			}
		}
		
		return $categories;
	}
	
	function getCategoriesPath($categories_id, $include_self = true) {
		$categories = $this->getCategoriesPathA($categories_id, '', $include_self);
		if( is_array($categories) ) {
			return array_reverse($categories);
		}
		
		return '';
	}
	
	function getCategoriesPathText($categories_id, $sep = '>>', $link = '') {
		$text = '';
		$categories = $this->getCategoriesPath($categories_id);
		if( is_array($categories) ) {
			for($i=0, $n=sizeof($categories); $i<$n; $i++) {
				if( $i > 0 ) {
					$text .= $sep;
				}
				if( $link != '' ) {
					$text .= '<a href="' . sprintf($link, $categories[$i]['categories_link']) . '">';
				}
				$text .= $categories[$i]['categories_name'];
				if( $link != '' ) {
					$text .= '</a>';
				}
			}
		}
		
		return $text;
	}
	
	function getCategoriesPathTextA($categories_id, $sep = '>>', $link = '') {
		$texta = array();
		$categories = $this->getCategoriesPath($categories_id);
		if( is_array($categories) ) {
			for($i=0, $n=sizeof($categories); $i<$n; $i++) {
				$texta[] = array(
						'link' => (($link == '') ? $categories[$i]['categories_link'] : sprintf($link, $categories[$i]['categories_link'])),
						'text' => $categories[$i]['categories_name'],
							);
			}
		}
		
		return $texta;
	}
	
	//
	function addCategory($data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array['categories_owner'] = $this->owner;
		$data_array['categories_date_added'] = 'now()';
		$data_array['categories_added_by'] = $this->_get_current_user();
		$data_array['categories_status'] = '1';
		
		$categories_id = $this->_insert($this->table, $data_array);
		if( $categories_id > 0 && ($this->_not_null($data_array['categories_link']) == false) ) {
			$this->_generate_category_link($categories_id, $data_array['categories_name']);
		}
		
		return $categories_id;
	}
	
	function addCategoryB($categories_name, $categories_description = '') {
		$category = $this->getCategory($categories_name, 'name', -1);
		if( is_array($category) && $category['categories_id'] > 0 ) {
			$categories_id = $category['categories_id'];
		} else {
			$categories_id = $this->addCategoryD($categories_name, $categories_description);
		}
		
		return $categories_id;
	}
	
	function addCategoryD($categories_name, $categories_description = '') {
		$data_array = array(
					'categories_parent' => '0',
					'categories_name' => $categories_name,
					'categories_description' => $categories_description,
						);
		$categories_id = $this->addCategory($data_array);
		
		return $categories_id;
	}
	
	function addCategoryCount($categories_id) {
		return $this->adb->update("update " . $this->table . " set categories_count = categories_count + 1 where categories_id = '" . (int)$categories_id . "'");
	}
	
	function addCategories($data_array) {
		$categories_ids = array();
		if( is_array($data_array) ) {
			foreach($data_array as $k => $v) {
				$categories_ids[] = $this->addCategory($v);
			}
		}
		
		return $categories_ids;
	}
	
	function updateCategory($categories_id, $data_array) {
		$data_array = func_db_input($data_array);
		
		$data_array['categories_last_modified'] = 'now()';
		$data_array['categories_modified_by'] = $this->_get_current_user();
		
		return $this->_update($this->table, $data_array, "categories_id = '" . (int)$categories_id . "'");
	}
	
	function updateCategoryLink($categories_id, $categories_link, $check = 0, $filter = 0) {
		if( $filter > 0 ) {
			$categories_link = $this->_filter_link($categories_link);
		}
		if( $check > 0 ) {
			$category = $this->getCategory($categories_link, 'link', -1, $categories_id);
			if( $category['categories_id'] > 0 ) {
				return -2;
			}
		}
		if( $categories_link == '' ) {
			return -1;
		}
		$categories_link = func_db_input($categories_link);
		
		return $this->adb->query("update " . $this->table . " set categories_link = '" . $categories_link . "' where categories_id = '" . (int)$categories_id . "'");
	}
	
	function updateCategoryName($categories_id, $categories_name) {
		$categories_name = func_db_input($categories_name);
		
		$query = "update " . $this->table . " set categories_name = '" . $categories_name . "' where categories_id = '" . (int)$categories_id . "'";
		return $this->adb->update($query);
	}
	
	function updateCategoryDescription($categories_id, $categories_description) {
		$categories_description = func_db_input($categories_description);
		
		$query = "update " . $this->table . " set categories_description = '" . $categories_description . "' where categories_id = '" . (int)$categories_id . "'";
		return $this->adb->update($query);
	}
	
	function updateCategoryDetail($categories_id, $categories_name, $categories_description) {
		$categories_name = func_db_input($categories_name);
		$categories_description = func_db_input($categories_description);
		
		$query = "update " . $this->table . " set categories_name = '" . $categories_name . "', categories_description = '" . $categories_description . "' " 
				. " where " . $this->_query_owner_rt() . " and categories_id = '" . (int)$categories_id . "'";
		return $this->adb->update($query);
	}
	
	function deleteCategory($categories_id) {
		$query = "delete from " . $this->table . " where " . $this->_query_owner_rt() . " and categories_id = '" . (int)$categories_id . "'";
		return $this->adb->delete($query);
	}
	
	function deleteCategories($categories_ids) {
		if( is_array($categories_ids) ) {
			if( sizeof($categories_ids) > 30 ) {
				$results = true;
				foreach($categories_ids as $k => $categories_id) {
					$result = $this->deleteCategory($categories_id);
					if( $result == false ) {
						$results = false;
					}
				}
				
				return $results;
			} else {
				$query_ids = "'" . implode("', '", array_values($categories_ids)) . "'";
				$query = "delete from " . $this->table . " where " . $this->_query_owner_rt() . " and categories_id in (" . $query_ids . ")";
				return $this->adb->delete($query);
			}
		}
	}
	
	function setCategoriesStatus($categories_ids, $status = 1) {
		if( is_array($categories_ids) ) {
			foreach($categories_ids as $k => $categories_id) {
				$this->updateCategory($categories_id, array('categories_status' => $status));
			}
		} else {
			$this->updateCategory($categories_ids, array('categories_status' => $status));
		}
	}
	
	//
	function check($categories_id) {
		$count = $this->adb->getCountA($this->table, "categories_id = '" . (int)$categories_id . "'");
		
		return ($count > 0) ? true : false;
	}
	
	function get($categories_id) {
		return $this->adb->getRowA($this->table, "categories_id = '" . (int)$categories_id . "'");
	}
	
	function add($data_array) {
		if( !$this->_not_null($data_array) || !is_array($data_array) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array['categories_date_added'] = 'now()';
		$data_array['categories_added_by'] = $this->_get_current_user();
		$data_array['categories_status'] = '1';
		
		return $this->adb->insertA($this->table, $data_array);
	}
	
	function update($categories_id, $data_array) {
		if( !$this->_not_null($categories_id) ) {
			return false;
		}
		
		$data_array = func_db_input($data_array);
		$data_array['categories_last_modified'] = 'now()';
		$data_array['categories_modified_by'] = $this->_get_current_user();
		
		return $this->adb->updateA($this->table, $data_array, "categories_id = '" . (int)$categories_id . "'");
	}
	
	function delete($categories_id) {
		return $this->adb->deleteA($this->table, "categories_id = '" . (int)$categories_id . "'");
	}
	
	//private	
	function _generate_category_link($categories_id, $categories_name = '', $categories_link = '') {
		if( $categories_link == '' && $categories_name != '' ) {
			$categories_link = $this->_filter_link($categories_name);
		}
		if( $this->_not_null($categories_link) ) {
			$categories_link = func_db_input($categories_link);
			$query = "select count(*) as count from " . $this->table . " " 
					. " where categories_id != '" . (int)$categories_id . "' and categories_link = '" . $categories_link . "'";
			$count = $this->adb->getCount($query);
			if( $count == 0 ) {
				$this->updateCategoryLink($categories_id, $categories_link);
				return true;
			}
		}
		$categories_link = $this->_filter_link($categories_name);
		$categories_link = $this->_rand_link($categories_id, $categories_link);
		
		$this->updateCategory($categories_id, array('categories_link' => $categories_link));
		$this->_generate_category_link($categories_id, $categories_name, $categories_link);
		
		return true;
	}
	
	function _get_query_str($filter = '', $check = 0) {
		$query_str = "";
		if( $check == -1 ) {
			//
		} else {
			$query_str .= " and categories_status = 1";
		}
		if( is_array($filter) ) {
			$filter = func_db_input($filter);
			if( is_array($this->filter_query) ) {
				$filter = array_merge($filter, $this->filter_query);
			}
			//
			if( isset($filter['owner']) ) {
				$query_str .= " and categories_owner = '" . (int)$filter['owner'] . "' ";
			}
			if( isset($filter['keyword']) && $this->_not_null($filter['keyword']) == true ) {
				$query_str .= " and categories_name like '%" . $filter['keyword'] . "%' ";
			}
			if( isset($filter['parent']) ) {
				$query_str .= " and categories_parent = '" . (int)$filter['parent'] . "' ";
			}
			if( isset($filter['ids']) && $this->_not_null($filter['ids']) == true ) {
				if( is_array($filter['ids']) ) {
					$query_str .= " and categories_id in (" . $this->_implode(', ', $filter['ids']) . ") ";
				} else {
					$query_str .= " and categories_id = '" . (int)$filter['ids'] . "' ";
				}
			}
		}
		
		return $query_str;
	}
	
	function _get_query_str_custom($filter = '', $check = 0) {
		return '';
	}
	
	function _query_owner_rt($pf = '') {
		if( $this->_table_owner_usable == true ) {
			if( $this->_table_owner_prefix == '=' ) {
				$p = '';
			} elseif( $this->_table_owner_prefix == '+' ) {
				$p = $this->prefix;
			} elseif( $this->_table_owner_prefix == '' ) {
				$p = $this->prefix;
			} else {
				$p = $this->_table_owner_prefix;
			}
			if( $pf != '' ) {
				$p = $pf . '.' . $p;
			}
			
			return $this->_query_owner($p . $this->_table_owner_field);
		}
		
		return " 1 ";
	}
}
//
?>